home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / site.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-11  |  7.3 KB  |  346 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.1.1.1
  4.  * 1993/02/11 18:02:53
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/site.c,v
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: site.c
  14.  * Routines to build up a table of hostnames and access levels
  15.  *********************************************************************
  16.  * Revision History:
  17.  * site.c,v
  18.  * Revision 3.1.1.1  1993/02/11  18:02:53  lindner
  19.  * Gopher+1.2beta release
  20.  *
  21.  * Revision 1.2  1993/02/09  22:29:23  lindner
  22.  * added util.h to includes
  23.  *
  24.  * Revision 1.1  1992/12/10  23:13:27  lindner
  25.  * gopher 1.1 release
  26.  *
  27.  *
  28.  *********************************************************************/
  29.  
  30. #include "site.h"
  31. #include "Malloc.h"
  32. #include "ctype.h"
  33. #include "util.h"
  34.  
  35. extern boolean DEBUG;
  36.  
  37. static Site *
  38. SiteNew()
  39. {
  40.      Site *temp;
  41.  
  42.      temp = (Site *) malloc(sizeof(Site));
  43.  
  44.      if (temp == NULL)
  45.       return(NULL);
  46.  
  47.      temp->domain = STRnew();
  48.      STRinit(temp->domain);
  49.  
  50.      temp->Level = ACC_FULL;
  51.      temp->isnum = FALSE;
  52.  
  53.      return(temp);
  54. }
  55.  
  56. static void 
  57. SiteDestroy(site)
  58.   Site *site;
  59. {
  60.      STRdestroy(site->domain);
  61.      free(site);
  62. }
  63.  
  64. static void
  65. Sitecpy(site1, site2)
  66.   Site *site1, *site2;
  67. {
  68.      STRcpy(site1->domain, site2->domain);
  69.      
  70.      site1->Level = site2->Level;
  71.      site1->isnum = site2->isnum;
  72. }
  73.  
  74. static void
  75. SiteSet(site, dom, access)
  76.   Site *site;
  77.   char *dom;
  78.   Accesslevel access;
  79. {
  80.      STRset(site->domain, dom);
  81.      site->Level = access;
  82.      
  83.      if (isdigit(*dom))
  84.       site->isnum = TRUE;
  85.      else
  86.       site->isnum = FALSE;
  87. }
  88.  
  89. /******************************************************/
  90.  
  91. SiteArray *
  92. SiteArrayNew()
  93. {
  94.      SiteArray *temp;
  95.      
  96.      temp = DAnew(20, SiteNew, NULL, SiteDestroy, Sitecpy);
  97.      
  98.      return(temp);
  99. }
  100.  
  101. void
  102. SiteArrayAdd(sitearr, name, Level)
  103.   SiteArray *sitearr;
  104.   char *name;
  105.   Accesslevel Level;
  106. {
  107.      Site *temp;
  108.  
  109.      temp = SiteNew();
  110.  
  111.      SiteSet(temp, name, Level);
  112.  
  113.      SiteArrPush(sitearr, temp);
  114.      return;
  115. }
  116.  
  117.  
  118. Accesslevel
  119. SiteAccess(sitearr, name)
  120.   SiteArray *sitearr;
  121.   char *name;
  122. {
  123.      int i;
  124.      Site *temp;
  125.  
  126.      if (name == NULL)
  127.       return(ACC_UNKNOWN);  /*** ??? We need to compare *something* ***/
  128.  
  129.      for (i=0; i< DAgetTop(sitearr); i++) {
  130.       
  131.       temp = SiteArrgetEntry(sitearr, i);
  132.  
  133.       if (DEBUG) 
  134.            printf("Testing for %s\n", STRget(temp->domain));
  135.  
  136.       if (temp->isnum == TRUE) {
  137.            /*** Check for a match from the beginning ***/
  138.            int namelen, domainlen;
  139.  
  140.            namelen = strlen(name);
  141.            domainlen = strlen(STRget(temp->domain));
  142.  
  143.            if (namelen >domainlen)
  144.             namelen = domainlen;
  145.  
  146.            if (strncmp(name, STRget(temp->domain), namelen) == 0)
  147.             return(temp->Level);
  148.       } else {
  149.            /*** Check for a match from the end ***/
  150.            /*** It's a domain name ***/
  151.            int namelen, domainlen;
  152.  
  153.            namelen = strlen(name);
  154.            domainlen = strlen(STRget(temp->domain));
  155.  
  156.  
  157.            /*** don't compare if incoming name is shorter than domain ***/
  158.            if (domainlen <= namelen)
  159.             if (strcasecmp((name+namelen-domainlen), 
  160.                    STRget(temp->domain))==0)
  161.              return(temp->Level);
  162.            
  163.       }
  164.      }
  165.  
  166.      /*** Hmmm, didn't find a match, return -1 ***/
  167.      return(ACC_UNKNOWN);
  168. }
  169.  
  170.  
  171. boolean
  172. SiteArrCanRead(sitearr, hostname, ipnum)
  173.   SiteArray *sitearr;
  174.   char *hostname, *ipnum;
  175. {
  176.      Accesslevel level;
  177.  
  178.      if (hostname != NULL) {
  179.       level = SiteAccess(sitearr, hostname);
  180.       if (DEBUG) printf("Host siteaccess %d\n", level);
  181.       if ((level & ACC_READ) == ACC_READ && level != ACC_UNKNOWN)
  182.            return(TRUE);
  183.       else if (level != ACC_UNKNOWN)
  184.            return(FALSE);
  185.      }
  186.      
  187.      /** Test the ipnum second **/
  188.      if (ipnum != NULL) {
  189.       level = SiteAccess(sitearr, ipnum);
  190.       if (DEBUG) printf("IPnum siteaccess %d\n", level);
  191.       if ((level & ACC_READ) == ACC_READ && level != ACC_UNKNOWN)
  192.            return(TRUE);
  193.       else if (level != ACC_UNKNOWN)
  194.            return(FALSE);
  195.      }
  196.  
  197.      /*** No matches found, so let's return the default ***/
  198.  
  199.      return(ACC_UNKNOWN);
  200. }
  201.  
  202.  
  203.  
  204. boolean
  205. SiteArrCanBrowse(sitearr, hostname, ipnum)
  206.   SiteArray *sitearr;
  207.   char *hostname, *ipnum;
  208. {
  209.      Accesslevel level;
  210.  
  211.      if (hostname != NULL) {
  212.       level = SiteAccess(sitearr, hostname);
  213.       if (DEBUG) printf("Host siteaccess %d\n", level);
  214.       if ((level & ACC_BROWSE) == ACC_BROWSE && level != ACC_UNKNOWN)
  215.            return(TRUE);
  216.       else if (level != ACC_UNKNOWN)
  217.            return(FALSE);
  218.      }
  219.      
  220.      /** Test the ipnum second **/
  221.      if (ipnum != NULL) {
  222.       level = SiteAccess(sitearr, ipnum);
  223.       if (DEBUG) printf("IPnum siteaccess %d\n", level);
  224.       if ((level & ACC_BROWSE) == ACC_BROWSE && level != ACC_UNKNOWN)
  225.            return(TRUE);
  226.       else if (level != ACC_UNKNOWN)
  227.            return(FALSE);
  228.      }
  229.  
  230.      if (DEBUG)  printf("No matches...\n");
  231.      /*** No matches found, so let's return unknown ***/
  232.      return(ACC_UNKNOWN);
  233. }
  234.  
  235.  
  236.  
  237. boolean
  238. SiteArrCanSearch(sitearr, hostname, ipnum)
  239.   SiteArray *sitearr;
  240.   char *hostname, *ipnum;
  241. {
  242.      Accesslevel level;
  243.  
  244.      if (hostname != NULL) {
  245.       level = SiteAccess(sitearr, hostname);
  246.       if (DEBUG) printf("Host siteaccess %d\n", level);
  247.       if ((level & ACC_SEARCH) == ACC_SEARCH && level != ACC_UNKNOWN)
  248.            return(TRUE);
  249.       else if (level != ACC_UNKNOWN)
  250.            return(FALSE);
  251.      }
  252.      
  253.      /** Test the ipnum second **/
  254.      if (ipnum != NULL) {
  255.       level = SiteAccess(sitearr, ipnum);
  256.       if (DEBUG) printf("IPnum siteaccess %d\n", level);
  257.       if ((level & ACC_SEARCH) == ACC_SEARCH && level != ACC_UNKNOWN)
  258.            return(TRUE);
  259.       else if (level != ACC_UNKNOWN)
  260.            return(FALSE);
  261.      }
  262.  
  263.      if (DEBUG)  printf("No matches...\n");
  264.      /*** No matches found, so let's return the default ***/
  265.  
  266.      return(ACC_UNKNOWN);
  267.  
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. /*
  277.  * process a site description line.
  278.  */
  279.  
  280. boolean
  281. SiteProcessLine(sitearr, inputline, defaccess)
  282.   SiteArray   *sitearr;
  283.   char        *inputline;
  284.   Accesslevel defaccess;
  285. {
  286.      char name[256];
  287.      char *namep = name;
  288.      Accesslevel level = defaccess;
  289.      
  290.      /*** the first word is the domain/ip# ***/
  291.      while (*inputline == ' ' || *inputline == '\t')
  292.       inputline++;
  293.  
  294.      while (*inputline != ' ' && *inputline != '\t') {
  295.       *namep = *inputline;
  296.       namep++;
  297.       inputline++;
  298.      }
  299.      
  300.      *namep = '\0';  /*** Terminate it ***/
  301.  
  302.      if (namep == name)
  303.       return(FALSE); /*** bad line ***/
  304.      
  305.      while (*inputline != '\0') {
  306.  
  307.       if (*inputline == ' ' || *inputline == '\t' || *inputline == ',') {
  308.            inputline++;
  309.            if (*inputline == '!')
  310.             inputline++;
  311.  
  312.            switch (*inputline) {
  313.  
  314.            case 'b':
  315.             /** Browse **/
  316.             if (*(inputline -1) == '!')
  317.              level &= (ACC_SEARCH | ACC_READ);
  318.             else
  319.              level |= ACC_BROWSE;
  320.             break;
  321.            case 'r':
  322.             /*** Read ***/
  323.             if (*(inputline -1) == '!')
  324.              level &= (ACC_BROWSE | ACC_SEARCH);
  325.             else
  326.              level |= ACC_READ;
  327.             break;
  328.  
  329.            case 's':
  330.             /*** Search ***/
  331.             if (*(inputline -1) == '!')
  332.              level &= (ACC_BROWSE | ACC_READ);
  333.             else
  334.              level |= ACC_SEARCH;
  335.             break;
  336.            }
  337.       } else
  338.            inputline++;
  339.       
  340.      }
  341.  
  342.      SiteArrayAdd(sitearr, name, level);
  343.      return(TRUE);
  344. }
  345.  
  346.